-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
WING_D = {}

function WING_D:is_wing_opend(player)
    local common_config = IMPORT_D:query("wingcommon")
    if not common_config then
        return false
    end

    ---! 翅膀功能对应解锁关卡
    if player:get_stage_level() < common_config.wingUnlock then
        return false
    end

    return true
end

---! 查找翅膀对象
function WING_D:find_wing(wingid, winglev)
    local configs = IMPORT_D:query("wing")
    if not configs then
        return
    end

    local config = configs[wingid]
    if not config then
        return
    end

    local wing = config[winglev or 1]
    if not wing then
        return
    end

    return wing
end

---! 改变翅膀
function WING_D:change_wing(player, wingid)
    local winglev = player:get_wing_level(wingid)
    if not winglev then
        ---! 尚未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CWingChange", result)
        return
    end

    ---! 修改当前玩家翅膀
    player:set_use_wing(wingid)

    local result = {}
    result.error = 0
    result.wingid = wingid
    player:send_packet("MSGS2CWingChange", result)
end

---! 提升翅膀
function WING_D:improve_wing_level(player, wingid)
    local winglev = player:get_wing_level(wingid)
    if not winglev then
        ---! 尚未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CWingLevelup", result)
        return
    end

    local wing = WING_D:find_wing(wingid, winglev)
    if not wing then
        ---! 翅膀配置不存在
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CWingLevelup", result)
        return
    end

    local next_wing = WING_D:find_wing(wingid, wing.level + 1)
    if not next_wing then
        ---! 当前翅膀已满级
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CWingLevelup", result)
        return
    end

    if player:get_currency(GamePropIds.kGamePropIdsDewIcon) < wing.useDew then
        ---! 所需仙露不足
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CWingLevelup", result)
        return
    end

    ---! 扣除所需仙露
    player:cost_currency(GamePropIds.kGamePropIdsDewIcon, wing.useDew)

    ---! 提升当前翅膀
    player:set_wing_level(wingid, next_wing.level)

    ---! 通知提升成功
    local result = {}
    result.error = 0
    result.id = wingid
    player:send_packet("MSGS2CWingLevelup", result)

    ---! 刷新仙露数量
    ITEM_D:send_currency_info(player)

    ---! 刷新翅膀列表
    WING_D:send_wing_info(player)

    ---! 同步翅膀排名
    RANK_D:sync_rank_data(player, "wings_rank")
end

---! 解锁翅膀
function WING_D:unlock_wing(player, wingid)
    if not WING_D:is_wing_opend(player) then
        ---! 翅膀功能未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CWingUnlock", result)
        return
    end

    local wing = WING_D:find_wing(wingid)
    if not wing then
        ---! 翅膀配置不存在
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CWingUnlock", result)
        return
    end

    local winglev = player:get_wing_level(wingid)
    if winglev then
        ---! 已经解锁
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CWingUnlock", result)
        return
    end

    if wing.unlocktype ~= 2 then
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CWingUnlock", result)
        return
    end

    ---! 钻石解锁
    if player:get_currency(GamePropIds.kGamePropIdsDiamondsIcon) < wing.unlockneed then
        ---! 需要仙露不足
        local result = {}
        result.error = 5
        player:send_packet("MSGS2CWingUnlock", result)
        return
    end

    ---! 扣除相应仙露
    player:cost_currency(GamePropIds.kGamePropIdsDiamondsIcon, wing.unlockneed)

    ---! 解锁当前翅膀
    player:set_wing_level(wingid, wing.level)

    ---! 通知解锁成功
    local result = {}
    result.error = 0
    result.id = wingid
    player:send_packet("MSGS2CWingUnlock", result)

    ---! 刷新仙露数量
    ITEM_D:send_currency_info(player)

    ---! 刷新翅膀列表
    WING_D:send_wing_info(player)

    ---! 同步翅膀排名
    RANK_D:sync_rank_data(player, "wings_rank")
end

---! 通过关卡解锁翅膀
function WING_D:unlock_wing_by_stage(player, stage_level)
    if not WING_D:is_wing_opend(player) then
        ---! 翅膀功能未解锁
        return
    end

    local configs = IMPORT_D:query("wing")
    if not configs then
        return
    end

    local wingids = {}
    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 1 then
            break
        end

        if stage_level <= config.unlockneed then
            break
        end

        wingids[config.id] = 1
    until true end

    if table.size(wingids) > 0 then
        ---! 解锁翅膀等级
        for wingid, winglev in pairs(wingids) do
            player:set_wing_level(wingid, winglev)
        end

        ---! 刷新翅膀列表
        WING_D:send_wing_info(player)
    end
end

---! 通过天数解锁翅膀
function WING_D:unlock_wing_by_days(player, days)
    if not WING_D:is_wing_opend(player) then
        ---! 翅膀功能未解锁
        return
    end

    local configs = IMPORT_D:query("wing")
    if not configs then
        return
    end

    local wingids = {}
    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 3 then
            break
        end

        if days < config.unlockneed then
            break
        end

        wingids[config.id] = 1
    until true end

    if table.size(wingids) > 0 then
        ---! 解锁翅膀等级
        for wingid, winglev in pairs(wingids) do
            player:set_wing_level(wingid, winglev)
        end

        ---! 刷新翅膀列表
        WING_D:send_wing_info(player)

        ---! 同步翅膀排名
        RANK_D:sync_rank_data(player, "wings_rank")
    end
end

---! 开启翅膀功能
function WING_D:init_wing_info(player)
    if not WING_D:is_wing_opend(player) then
        return
    end

    local configs = IMPORT_D:query("wing")
    if not configs then
        return
    end

    local open_wing = false
    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 4 then
            break
        end

        local winglev = player:get_wing_level(config.id)
        if winglev then
            break
        end

        ---! 解锁翅膀并开放等级
        player:set_wing_level(config.id, 1)

        ---! 修改翅膀标记
        open_wing = true
    until true end

    if not open_wing then
        return
    end

    ---! 刷新翅膀列表
    WING_D:send_wing_info(player)

    ---! 开启翅膀副本
    WINGSTAGE_D:init_wingstage_info(player)

    ---! 刷新翅膀福利
    WELFARE_D:send_tomorrow_data(player)
end

function WING_D:send_wing_info(player)
    local wings = {}
    local wing_tbl = player:query("wings") or {}
    for wingid, level in pairs(wing_tbl) do
        table.insert(wings, {
            id = tonumber(wingid),
            level = level,
        })
    end

    local result = {}
    result.wings = wings
    player:send_packet("MSGS2CWingList", result)
end
